//
//  CommitFileSelectionTableViewDataSource.swift
//  GitTFGUI
//
//  Created by Samuel Richard Magny on 2015-09-07.
//  Copyright (c) 2015 Samuel Richard Magny. All rights reserved.
//

import Cocoa
import TFSClient
import SwiftTools

class FilesTableViewDataSource: NSObject, NSTableViewDataSource {

    internal var source:OrderedDictionary<String, TFSFile> = [:]
    
    
    func numberOfRowsInTableView(tableView: NSTableView) -> Int
    {
        return self.source.count
    }
    
    func tableView(tableView: NSTableView, objectValueForTableColumn tableColumn: NSTableColumn?, row: Int) -> AnyObject?
    {
        let data = getDataAtRow(row)
        
        if(tableColumn?.identifier == FilesTableView.CheckColumnIdentifier)
        {
            return isRowSelected(row)
        }
        
        if(tableColumn?.identifier == FilesTableView.StatusColumnIdentifier)
        {
            let statusCell:NSTextFieldCell = tableColumn!.dataCell as! NSTextFieldCell
            
            switch(data.status) {
            case TFSFile.FileStatus.Removed:
                statusCell.textColor = NSColor.redColor()
            case TFSFile.FileStatus.Added:
                statusCell.textColor = NSColor.greenColor()
            case TFSFile.FileStatus.Modified:
                statusCell.textColor = NSColor.blackColor()
            case TFSFile.FileStatus.Untracked:
                statusCell.textColor = NSColor.purpleColor()
            default:
                statusCell.textColor = NSColor.blackColor()
            }
            
            return data.status.description
        }
        
        if(tableColumn?.identifier == FilesTableView.FileNameColumnIdentifier)
        {
            return data.fileName
        }
        
        if(tableColumn?.identifier == FilesTableView.FilePathColumnIdentifier)
        {
            return data.filePath
        }
        
        return ""
    }
    
    func tableView(tableView: NSTableView, setObjectValue object: AnyObject?, forTableColumn tableColumn: NSTableColumn?, row: Int) {
        
        if(tableColumn?.identifier == FilesTableView.CheckColumnIdentifier)
        {
            selectRow(tableView, row: row, setSelected: !isRowSelected(row))
        }
    }
    
    func selectAllRows(tableView:NSTableView)
    {
        tableView.reloadData()
    }
    
    func selectRow(tableView:NSTableView, row:Int, setSelected:Bool = true)
    {
        if let filesTableView = tableView as? FilesTableView
        {
            if let tfsClient = filesTableView.tfsClient
            {
                let file  = getDataAtRow(row)
                tfsClient.stageFile(file)
                
                let rowIndexes = NSIndexSet(index: 0)
                let columnIndexes = NSIndexSet(index:0)
                tableView.reloadDataForRowIndexes(rowIndexes, columnIndexes: columnIndexes)
            }
        }

    }
   
    
    //Sorting
    //----------------------------------------------------------------
    func tableView(tableView: NSTableView, sortDescriptorsDidChange oldDescriptors: [NSSortDescriptor])
    {
        let descriptor: NSSortDescriptor = tableView.sortDescriptors[0] 
        let isAscending = descriptor.ascending
        if(descriptor.key == "status") {
            if(isAscending) {
                source.sortInPlace({$0.1.status.description < $1.1.status.description})
            }
            else {
                source.sortInPlace({$0.1.status.description > $1.1.status.description})
            }
        }
        else if(descriptor.key == "fileName") {
            if(isAscending) {
                source.sortInPlace({$0.1.fileName < $1.1.fileName})
            }
            else {
                source.sortInPlace({$0.1.fileName > $1.1.fileName})
            }
        }
        else if(descriptor.key == "filePath") {
            if(isAscending) {
                source.sortInPlace({$0.1.filePath < $1.1.filePath})
            }
            else {
                source.sortInPlace({$0.1.filePath > $1.1.filePath})
            }
        }

        tableView.reloadData()
    }
    
    
    
    //Row Selection
    //----------------------------------------------------------------
    private func isRowSelected(row:Int)->Bool
    {
        let data  = getDataAtRow(row)
        return data.staged
    }
    
    private func getDataAtRow(row:Int)->TFSFile
    {
        return source[row].1 as TFSFile
    }
    
    
    
    
    

    
    
}
